<script setup lang="ts">
import { ref } from "@vue/runtime-core";
import Toast from "vant/lib/toast";
import "vant/es/toast/style";
import { useRouter } from "vue-router";

const router = useRouter();
const name = ref("");
const password = ref("");
const mail = ref("");
const tel = ref("");

const handlefinish = () => {
  const regEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  const reg = /^[1]([3-9])[0-9]{9}$/

  if (localStorage["name"] === name.value) {
    Toast("用户名已存在");
  } else if (name.value === "") {
    Toast("用户名不能为空");
  } else if (password.value === "") {
    Toast("密码不能为空");
  } else if (mail.value === "") {
    Toast("邮箱不能为空");
  } else if (regEmail.test(mail.value) === false) {
    Toast("邮箱验证失败");
    return;
  } else if (tel.value === "") {
    Toast("手机号不能为空");
  } else if (reg.test(tel.value) === false) {
    Toast("手机号验证失败");
  } else {
    localStorage.setItem("name", name.value);
    localStorage.setItem("password", password.value);
    localStorage.setItem("mail", mail.value);
    localStorage.setItem("tel", tel.value);
    localStorage.setItem("s", "true");
    Toast("注册成功");
    router.replace("/login"); //完成注册后跳转至登录页面
  }
};
</script>



<template>
  <div id="background">
    <div id="contain">
      <div class="form">
        <label>用户名：</label><input type="text" v-model.trim="name" /><br />
      </div>
      <div class="form">
        <label>密码：</label><input type="password" v-model.trim="password" /><br />
      </div>
      <div class="form">
        <label>邮箱：</label><input type="email" v-model.trim="mail" /><br />
      </div>
      <div class="form">
        <label>手机号：</label><input type="tel" v-model.trim="tel" /><br />
      </div>
      <button @click.prevent="handlefinish">提交</button>
    </div>
  </div>
</template>



<style scoped>
#background {
  width: 100%;
  height: 100%;
  /* background: url("../assets/bg2.jpg"); */
  background-size: 100% 100%;
  position: fixed;
  top: 0;
  left: 0;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
  user-select: none;
}

#contain {
  width: 580px;
  height: 560px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  border-radius: 20px;
}

#contain h1 {
  color: white;
}

.form {
  color: white;
  margin-left: 20%;
  margin-top: 60px;
  font-size: 20px;
  text-align: left;
  display: flex;
  align-items: center;
}

label {
  width: 5em;
  margin-right: 1em;
}

input,
textarea {
  margin-left: 10px;
  padding: 4px;
  border: solid 1px #4e5ef3;
  outline: 0;
  font: normal 13px/100% Verdana, Tahoma, sans-serif;
  width: 200px;
  height: 20px;
  background: #f1f1f190;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
}

input:hover,
textarea:hover,
input:focus,
textarea:focus {
  border-color: #0d0aa1;
}

button {
  position: relative;
  height: 33px;
  width: 150px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  border-radius: 10px;
  margin-top: 38px;
  box-shadow: none;
  color: white;
  margin-left: 40px;
}
</style>
